const express = require('express');
const axios = require('axios');
const router = express.Router();
//跨域处理
router.all('*', function(req, res, next) {
     //配置跨域所允许的域名
    res.header("Access-Control-Allow-Origin", "*");
    //配置允许的请求头，若请求头中包含自定义请求头在这没配置，则请求不允许，
    //如果不配置，报错：Request header field Content-Type is not allowed by Access-Control-Allow-Headers.
    res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
    //配置允许的请求方法
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    //配置允许发送cookie，不过要注意浏览器发送cookie只允许同一个域名下 
    res.header("Access-Control-Allow-Credentials",true);
    //res.header("Content-Type", "application/json;charset=utf-8");
    if (req.method == "OPTIONS") res.send(200);/*让options请求快速返回*/
    else next();
    //next();
});

//首页
router.get('/', (req, res) => {
    res.render('index', {
        title:'首页',
        active:1
    })
});
//审核结果页
router.get('/result/:filename', (req, res) => {
    axios.get('http://www.hefatong.com/contract/diagnose', {
        params: {
            filename: req.params.filename
        }
    })
    .then(function(response) {
        console.log('====response=====',response);
        axios.get('http://www.hefatong.com/payment/wx/qrcode/'+response.data.out_trade_no, {
            params: {
                //filename: req.params.filename
            }
        })
        .then(function(data){
            console.log('====data=====', data, data.data.qr_code_path)
            res.render('result', {
                title: '审核结果',
                data:response.data,
                wxqr:data.data.qr_code_path,
                trade: response.data.out_trade_no
            });
        })
    })
    .catch(function(error) {
        console.log(error);
    });
    //res.redirect("/");
    
});
//找律师
router.get('/lawyer', (req, res) => {
    res.render('lawyer', {
        title:'找律师',
        active:3
    })
});
//法律顾问
router.get('/adviser', (req, res) => {
    res.render('adviser', {
        title:'法律顾问',
        active:4
    })
});
//合作伙伴
router.get('/partner', (req, res) => {
    res.render('partner', {
        title:'合作伙伴',
        active:5
    })
});
//关于我们
router.get('/about', (req, res) => {
    res.render('about', {
        title:'关于我们',
        active:6
    })
});
//管理员登录
router.get('/adminlogin', (req, res) => {
    res.render('adminlogin', {
        title:'登录'
    })
});
//查询列表页
router.get('/querylist/:timestamp', (req, res) => {
    if(Date.now() - parseInt(req.params.timestamp) >= 1800){
        res.redirect("/adminlogin");
    }else{
        res.render('querylist', {
            title:'查询'
        })
    }
});
module.exports = router;
